package org.ray.util;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;
import java.util.regex.Matcher;


public class DataUtils
{
	public static String formatDate(Date date, String format)
	{
		if (date != null)
		{
			SimpleDateFormat dateFormat = null;
			
			try
			{
				dateFormat = new SimpleDateFormat(format);
			}
			catch (Exception e)
			{}
			
			if (dateFormat != null)
			{
				return dateFormat.format(date);
			}
		}
		
		return null;
	}

	public static int checkNull (Object other1, Object other2)
	{
	    if (other1 == null)
	    {
	        if (other2 == null) //both arguments are null
	        {
	            return 0;
	        }
	        else
	        //other1 is null, other2 is not null
	        {
	            return -1;
	        }
	    }
	    else if (other2 == null) //other1 is not null, other2 is null
	    {
	        return 1;
	    }
	    else
	    //both other1 and other2 are not null
	    {
	        return 0;
	    }
	}
	
	
	public static String getToday ()
	{
	    return new java.text.SimpleDateFormat("EEEEEEEE MMMMMMMMM d yyyy").format(new java.util.Date());
	}
	
	public static boolean stringNullOrEmpty(String str)
	{
		return (str == null || str.length() == 0);
	}
	
	public static <T> boolean listEmpty(List<T> list)
	{
		return (list == null || list.size() == 0);
	}
	
	public static <K, V> boolean mapEmpty(Map<K, V> map)
	{
		return (map == null || map.size() == 0);
	}
	
	public static <T> boolean arrayEmpty(T[] at)
	{
		return (at == null || at.length == 0);
	}
	
	
	public static String getDayOfWeek (int day)
	{
	    switch (day)
	    {
	        case Calendar.SUNDAY:
	            return "Sunday";
	        case Calendar.MONDAY:
	            return "Monday";
	        case Calendar.TUESDAY:
	            return "Tuesday";
	        case Calendar.WEDNESDAY:
	            return "Wednesday";
	        case Calendar.THURSDAY:
	            return "Thursday";
	        case Calendar.FRIDAY:
	            return "Friday";
	        case Calendar.SATURDAY:
	            return "Saturday";
	        default:
	            return "";
	    }
	}
	
	
	public static String getMonth (int month)
	{
	    switch (month)
	    {
	        case Calendar.JANUARY:
	            return "January";
	        case Calendar.FEBRUARY:
	            return "February";
	        case Calendar.MARCH:
	            return "March";
	        case Calendar.APRIL:
	            return "April";
	        case Calendar.MAY:
	            return "May";
	        case Calendar.JUNE:
	            return "June";
	        case Calendar.JULY:
	            return "July";
	        case Calendar.AUGUST:
	            return "August";
	        case Calendar.SEPTEMBER:
	            return "September";
	        case Calendar.OCTOBER:
	            return "October";
	        case Calendar.NOVEMBER:
	            return "November";
	        case Calendar.DECEMBER:
	            return "December";
	        default:
	            return "";
	    }
	}
	
	public static String removeSpaces (String string)
	{
	    //System.out.println("Original string: '" + string + "'");
	    if (string != null)
	    {
	        Pattern p = Pattern.compile(" ");
	        Matcher m = p.matcher(string);
	
	        string = m.replaceAll("_");
	    }
	
	    //System.out.println("Altered string: '" + string + "'");
	    return string;
	}
	
	public static String escapeAmpersandForBrowserAddress (String string)
	{
	    //System.out.println("Original string: '" + string + "'");
	    if (string != null)
	    {
	        Pattern p = Pattern.compile("&");
	        Matcher m = p.matcher(string);
	
	        string = m.replaceAll("%26");
	    }
	
	    //System.out.println("Altered string: '" + string + "'");
	    return string;
	}
	
	
	public static String escapeApostrophe (String string)
	{
	    //System.out.println("Original string: '" + string + "'");
	    if (string != null)
	    {
	        Pattern p = Pattern.compile("'");
	        Matcher m = p.matcher(string);
	
	        string = m.replaceAll("''");
	    }
	
	    //System.out.println("Altered string: '" + string + "'");
	    return string;
	}
	
	public static String escapeComma (String string)
	{
	    //System.out.println("Original string: '" + string + "'");
	    if (string != null)
	    {
	        Pattern p = Pattern.compile(",");
	        Matcher m = p.matcher(string);
	
	        string = m.replaceAll("''");
	    }
	
	    //System.out.println("Altered string: '" + string + "'");
	    return string;
	}
	
	//Split given string into an ArrayList of substrings with length of 'chars'
	public static List<String> splitString (String string, int chars)
	{
		List<String> strings = new ArrayList<String>();
	    int size = string.length();
	
	    if (size <= chars)
	    {
	        strings.add(string);
	    }
	    else
	    {
	        int iterations = size/chars;
	        for (int i = 0; i<iterations; i++)
	        {
	            strings.add(string.substring( (i*chars), ( chars*(i+1) ) ));
	        }
	    }
	
	    return strings;
	}
	public static List<String> splitString (String string, String splitStr)
	{
		List<String> strings = new ArrayList<String>();
		if (!stringNullOrEmpty(string))
		{
			String[] splitedStrs = string.split(splitStr);
			if (!arrayEmpty(splitedStrs))
				return Arrays.asList(splitedStrs);
		}
	
	    return strings;
	}
    
//	public static Map<String, String> toKeyValue(String str, String sep)
//    {
//    	Map<String, String> map = new HashMap<String, String>();
//    	List<String> strPairs = splitString(str, sep);
//    	
//    	if (!listEmpty(strPairs) && strPairs.size() == 2)
//    	{
//    		map.put(strPairs.get(0), strPairs.get(1));
//    	}
//    	
//    	return map;
//    }
	
	//Count instances of substring in string
	public static int countSubstrings (String string, String substring)
	{
	    int count = 0;
	
	    int index = string.indexOf(substring);
	    int startPoint;
	
	    while (index != -1)
	    {
	        startPoint = index + (substring.length());
	        index = string.indexOf(substring, startPoint);
	        count++;
	    }
	
	    //System.out.println(substring + " occurs " + count + " times");
	
	    return count;
	}
	
	
	public static final String floatToPercent (Float flt)
	{
	    return new String(Math.round(flt.floatValue() * 100f) + "%");
	}
	
	
	public static final Integer[] intArrayToIntegerArray (int[] ints)
	{
	    Integer[] integers;
	
	    if (ints != null)
	    {
	        integers = new Integer[ints.length];
	
	        for (int i = 0; i < ints.length; i++)
	        {
	            integers[i] = new Integer(ints[i]);
	        }
	    }
	    else
	    {
	        integers = new Integer[0];
	    }
	
	    return integers;
	}
	
	public static final List<Integer> ToListOfIntegers (String[] stringArray)
	{
		List<Integer> integerList = new ArrayList<Integer>();
	    if (stringArray != null)
	    {
	        for (int i = 0; i < stringArray.length; i++)
	        {
	            integerList.add(new Integer(stringArray[i]));
	        }
	    }
	    return integerList;
	}
	
	public static String removeChar(String s, char c) 
	{
	    String r = "";
	
	    for (int i = 0; i < s.length(); i ++) 
	    {
	        if (s.charAt(i) != c) 
	        {
	            r += s.charAt(i);
	        }
	    }
	   return r;
	}
	
	//Converts a List of Integers to a comma-delimited String
	
	public static String getCommaDelimitedStringFromList (List<Integer> integerList)
	{
	    if (integerList == null || integerList.size() == 0)
	    {
	        return "";
	    }
	
	    StringBuffer sb = new StringBuffer();
	
	    for (Iterator<Integer> i = integerList.iterator(); i.hasNext(); )
	    {
	        Integer number = i.next();
	        sb.append(number);
	        if (i.hasNext())
	        {
	            sb.append(", ");
	        }
	    }
	
	    return sb.toString();
	}
	
	public static String replaceString (String string, String original, String replacement)
	{
	    //System.out.println("Original string: '" + string + "'");
	    if (string != null)
	    {
	        Pattern p = Pattern.compile(original);
	        Matcher m = p.matcher(string);
	
	        string = m.replaceAll(replacement);
	    }
	
	    //System.out.println("Altered string: '" + string + "'");
	    return string;
	}
	
	public static boolean truncate (String value, int maxChars)
	{
	    if (value.length() >= maxChars)
	    {
	        value = value.substring(0,maxChars) + "...";
	        return true;
	    }
	
	    return false;
	}

}